﻿package mortal.game.resource
{
    import Message.DB.Tables.*;
    import com.gengine.resource.*;
    import flash.utils.*;

    public class TaskConfig extends Object
    {
        private var _taskNames:Dictionary;
        private var _taskConfig:Dictionary;
        private var _taskDialog:Dictionary;
        private var _taskShow:Dictionary;
        private var _drama:Dictionary;
        private var _guide:Dictionary;
        private var _chapters:Array;
        private var _paths:Dictionary;
        private var _chaptersNums:Dictionary;
        private var _guildTask:Dictionary;
        private var _guildTaskColors:Dictionary;
        private static var _instance:TaskConfig;

        public function TaskConfig()
        {
            this._chaptersNums = new Dictionary();
            this.initConfig();
            return;
        }// end function

        public function getTaskConfigByCode(param1:int) : TTask
        {
            return this._taskConfig[param1];
        }// end function

        public function getDialog(param1:int) : TTaskDialog
        {
            return this._taskDialog[param1] as TTaskDialog;
        }// end function

        private function initConfig() : void
        {
            var _loc_2:Object = null;
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            var _loc_5:TTask = null;
            var _loc_6:TTaskDialog = null;
            var _loc_7:TTaskShow = null;
            var _loc_8:TTaskDrama = null;
            var _loc_9:TTaskGuide = null;
            var _loc_10:TTaskChapter = null;
            var _loc_11:TPath = null;
            var _loc_12:TTaskGuild = null;
            var _loc_13:Array = null;
            var _loc_14:Array = null;
            this._taskConfig = new Dictionary();
            var _loc_1:* = ConfigManager.instance.getJSONByFileName("t_task_config.json");
            for each (_loc_2 in _loc_1)
            {
                
                _loc_5 = new TTask();
                this.copyValue(_loc_5, _loc_2);
                this._taskConfig[_loc_5.code] = _loc_5;
            }
            this._taskDialog = new Dictionary();
            _loc_1 = ConfigManager.instance.getJSONByFileName("t_task_dialog.json");
            for each (_loc_2 in _loc_1)
            {
                
                _loc_6 = new TTaskDialog();
                this.copyValue(_loc_6, _loc_2);
                this._taskDialog[_loc_6.id] = _loc_6;
            }
            this._taskShow = new Dictionary();
            _loc_1 = ConfigManager.instance.getJSONByFileName("t_task_show.json");
            for each (_loc_2 in _loc_1)
            {
                
                _loc_7 = new TTaskShow();
                this.copyValue(_loc_7, _loc_2);
                this._taskShow[_loc_7.id] = _loc_7;
            }
            this._drama = new Dictionary();
            _loc_1 = ConfigManager.instance.getJSONByFileName("t_task_drama.json");
            for each (_loc_2 in _loc_1)
            {
                
                _loc_8 = new TTaskDrama();
                this.copyValue(_loc_8, _loc_2);
                this._drama[this.getDramaKey(_loc_8.dramaCode, _loc_8.step)] = _loc_8;
            }
            this._guide = new Dictionary();
            _loc_1 = ConfigManager.instance.getJSONByFileName("t_task_guide.json");
            for each (_loc_2 in _loc_1)
            {
                
                _loc_9 = new TTaskGuide();
                this.copyValue(_loc_9, _loc_2);
                var _loc_17:* = _loc_9;
                var _loc_18:* = _loc_9.step + 1;
                _loc_17.step = _loc_18;
                this._guide[_loc_9.id] = _loc_9;
            }
            this._chapters = [];
            _loc_1 = ConfigManager.instance.getJSONByFileName("t_task_chapter.json");
            for each (_loc_2 in _loc_1)
            {
                
                _loc_10 = new TTaskChapter();
                this.copyValue(_loc_10, _loc_2);
                this._chapters.push(_loc_10);
            }
            this._chapters.sort(this.sortChapter);
            _loc_3 = 0;
            _loc_4 = 0;
            while (_loc_4 < this._chapters.length)
            {
                
                _loc_10 = this._chapters[_loc_4];
                this._chaptersNums[_loc_10.id] = _loc_3;
                _loc_3 = _loc_3 + _loc_10.taskNum;
                _loc_4++;
            }
            this._paths = new Dictionary();
            _loc_1 = ConfigManager.instance.getJSONByFileName("t_path.json");
            for each (_loc_2 in _loc_1)
            {
                
                _loc_11 = new TPath();
                this.copyValue(_loc_11, _loc_2);
                this._paths[_loc_11.id] = _loc_11;
            }
            this._guildTask = new Dictionary();
            this._guildTaskColors = new Dictionary();
            _loc_1 = ConfigManager.instance.getJSONByFileName("t_task_guild.json");
            for each (_loc_2 in _loc_1)
            {
                
                _loc_12 = new TTaskGuild();
                this.copyValue(_loc_12, _loc_2);
                if (_loc_12.type == 2)
                {
                    this._guildTask[_loc_12.value] = _loc_12;
                    continue;
                }
                if (_loc_12.type == 3)
                {
                    _loc_13 = [];
                    _loc_14 = _loc_12.valueStr.split(",");
                    _loc_4 = 0;
                    while (_loc_4 < _loc_14.length)
                    {
                        
                        _loc_13.push(parseInt(_loc_14[_loc_4]) / 100);
                        _loc_4++;
                    }
                    this._guildTaskColors[int(_loc_12.value)] = _loc_13;
                }
            }
            return;
        }// end function

        public function getGuildTaskBaseReward(param1:int) : TTaskGuild
        {
            var _loc_2:* = param1;
            while (_loc_2 >= 0)
            {
                
                if (this._guildTask[_loc_2] != null)
                {
                    return this._guildTask[_loc_2] as TTaskGuild;
                }
                _loc_2 = _loc_2 - 1;
            }
            return null;
        }// end function

        public function getGuildTaskRewardTimes(param1:int) : Array
        {
            return this._guildTaskColors[param1];
        }// end function

        public function getPath(param1:int) : TPath
        {
            return this._paths[param1];
        }// end function

        private function sortChapter(param1:TTaskChapter, param2:TTaskChapter) : int
        {
            if (param1.id > param2.id)
            {
                return 1;
            }
            return -1;
        }// end function

        public function getChapter(param1:int) : TTaskChapter
        {
            var _loc_4:TTaskChapter = null;
            var _loc_2:int = 0;
            var _loc_3:int = 0;
            while (_loc_3 < this._chapters.length)
            {
                
                _loc_4 = this._chapters[_loc_3];
                _loc_2 = _loc_2 + _loc_4.taskNum;
                if (param1 > _loc_2)
                {
                }
                else
                {
                    return _loc_4;
                }
                _loc_3++;
            }
            return _loc_4;
        }// end function

        public function getChapterIndex(param1:TTaskChapter) : int
        {
            return this._chapters.indexOf(param1);
        }// end function

        public function getToChapterNeedNum(param1:int) : int
        {
            return this._chaptersNums[param1];
        }// end function

        private function getDramaKey(param1:int, param2:int) : String
        {
            return param1.toString() + "_" + param2.toString();
        }// end function

        public function getDrama(param1:int, param2:int) : TTaskDrama
        {
            return this._drama[this.getDramaKey(param1, param2)] as TTaskDrama;
        }// end function

        public function getTaskShow(param1:int) : TTaskShow
        {
            return this._taskShow[param1];
        }// end function

        public function getTaskGuide(param1:int) : TTaskGuide
        {
            return this._guide[param1] as TTaskGuide;
        }// end function

        public function getTaskGuideByTask(param1:String, param2:int, param3:Boolean = false) : TTaskGuide
        {
            var _loc_4:TTaskGuide = null;
            for each (_loc_4 in this._guide)
            {
                
                if (_loc_4.taskId.indexOf(param1) >= 0)
                {
                    if (param3)
                    {
                        if (_loc_4.step > 100)
                        {
                            return _loc_4;
                        }
                        continue;
                    }
                    if (_loc_4.step == param2)
                    {
                        return _loc_4;
                    }
                }
            }
            return null;
        }// end function

        private function copyValue(param1:Object, param2:Object) : void
        {
            var _loc_3:Object = null;
            for (_loc_3 in param2)
            {
                
                param1[_loc_3] = param2[_loc_3];
            }
            return;
        }// end function

        public static function get instance() : TaskConfig
        {
            if (_instance == null)
            {
                _instance = new TaskConfig;
            }
            return _instance;
        }// end function

    }
}
